const Service = require('egg').Service;

class ClassService extends Service {
  async index() {
    const { app } = this;
  }

  // 添加课程
  async addCourse() {
    const { ctx, app } = this;
    const { course_name } = ctx.request.body;
    const result = await app.mysql.get('course_info', { course_name })
    if (!result) {
      const data = await app.mysql.insert('course_info', {
        course_name
      })
      return { code: 0, data }
    } else {
      return { code: -1, msg: '课程名称重复，请换一个名字' }
    }

  }
  // 查询课程接口
  async findCourse() {
    const { app } = this;
    const data = await app.mysql.select('course_info')
    return { code: 0, data }
  }

  // 添加教室
  async addRoom() {
    const { ctx, app } = this;
    const { room_id } = ctx.request.body;
    const result = await app.mysql.get('room_info', { room_id })
    if (!result) {
      const data = await app.mysql.insert('room_info', {
        room_id
      })
      return { code: 0, data }
    } else {
      return { code: -1, msg: '教室名字重复，请换一个添加' }
    }

  }

  // 查询教室接口
  async findRoom() {
    const { ctx, app } = this;
    const data = await app.mysql.select('room_info')
    return { code: 0, data }
  }

  // 添加班级信息接口  根据查询到的课程和教室
  async addClassGrade() {
    const { ctx, app } = this;
    /** grade_name：班级，course_name：课程，classroom:教室  */
    const { grade_name, course_name, class_room } = ctx.request.body;
    const result = await app.mysql.get('class_grade', { grade_name })
    if (!result) {
      const data = await app.mysql.insert('class_grade', {
        grade_name, course_name, class_room
      })
      return { code: 0, data }
    } else {
      return { code: -1, msg: '班级已经存在，请换一个添加' }
    }
  }
  // 
}

module.exports = ClassService;